package com.ars.order;

import com.ars.db.DBTools;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;

import javax.swing.*;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TicketRefundController {
    @FXML
    private TextField tfTicketID;

    @FXML
    public void commitAction() {
        if (isExist()) {
            int choice = JOptionPane.showConfirmDialog(null,
                    "确认退票？",
                    "提示", JOptionPane.YES_NO_OPTION);
            if (choice == 0) {
                updateDB();
            }
        } else {
            JOptionPane.showMessageDialog(null, "操作失败，该机票不存在！请仔细核对", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    //检测机票是否存在
    private boolean isExist() {
        if (tfTicketID.getText().isEmpty()) {
            return false;
        }
        String sql = "SELECT * FROM orders WHERE oid = " + tfTicketID.getText();
        ResultSet rs = new DBTools().query(sql);
        try {
            if (rs.next()) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    //更新数据库
    private void updateDB() {
        String sql = "DELETE FROM orders WHERE oid = " + tfTicketID.getText();
        new DBTools().update(sql);
        JOptionPane.showMessageDialog(null, "操作成功", "提示", JOptionPane.INFORMATION_MESSAGE);
    }
}
